//
// C++ Implementation: selectedthreadlistwidget
//
// Description: 
//
//
// Author: Christophe Vandeplas <christophe@vandeplas.com>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//

#ifdef USE_LOADGEN_GUI


#include <iostream>
#include <string>
#include <QString>
#include <QStringList>
#include "selectedthreadlistwidget.h"
#include "Oxsipgen.h"
#include "eventhistory.h"

using std::string;

SelectedThreadListWidget::SelectedThreadListWidget(QWidget *parent)
 : QListWidget(parent)
{
    _selectedThreadNumber=-1;
}


SelectedThreadListWidget::~SelectedThreadListWidget()
{
}

void SelectedThreadListWidget::cleanUpList()
{
    while (count() > 0 ) {
        takeItem ( 0 );
    }
}

void SelectedThreadListWidget::updateListOfElements(const int threadId, const bool reloadAll)
{
    // remember what the selected thread is, to prevent interference with the other signals
    if ( _selectedThreadNumber == threadId || _selectedThreadNumber==-1 || reloadAll ) { 
        EventHistory *history = Oxsipgen::getInstance()->getHistoryOfThread(threadId);

        // works like this:
        //  IF reloadAll THEN 
        //    empty list
        //    fill the complete list insertItems()
        //  ELSE 
        //    get size of list count()
        //    fill list starting from previous item
        uint loopBegin=0;
        if (reloadAll)
        {
            cleanUpList();
            loopBegin=0;
        }
        else 
        {
            loopBegin = count();
        }
        // now fill the QStringList and add the items to the list
        QStringList sl;
        for (uint i =loopBegin; i < history->size() ; i++) {
            //std::cout << "QString " << (history.at(i)).c_str() << std::endl;
            sl << QString((history->at(i)).c_str());
        }
        insertItems( loopBegin, sl);
        _selectedThreadNumber= threadId;
        this->repaint();
        this->scrollToBottom();
    }
}

void SelectedThreadListWidget::getSelectedThreadId(int &threadId)
{
    threadId=_selectedThreadNumber;
}

#endif // USE_LOADGEN_GUI

